package system_smtp_service

import (
	"gitee.com/Luna-CY/Golang-Project-Template/internal/context"
	"gitee.com/Luna-CY/Golang-Project-Template/internal/errors"
	"gitee.com/Luna-CY/Golang-Project-Template/internal/logger"
	"gitee.com/Luna-CY/Golang-Project-Template/internal/util/pointer"
	"gitee.com/Luna-CY/Golang-Project-Template/model"
)

func (cls *Smtp) Cancel(ctx context.Context, log *model.SystemSmtpLog) errors.Error {
	if nil == log {
		logger.SugarLogger(logger.WithRequestId(ctx), logger.WithStack()).Errorf("ISS_TP.S_TP.C_EL.124139 log is nil")

		return errors.ErrorServerInternalError("ISS_TP.S_TP.C_EL.144200", "异常的传入参数")
	}

	if model.SystemSmtpLogSendTypeTime != *log.SendType {
		logger.SugarLogger(logger.WithRequestId(ctx), logger.WithStack()).Errorf("ISS_TP.S_TP.C_EL.192606 异常的发送类型，传入的发送类型为 %d", *log.SendType)

		return errors.ErrorServerInternalError("ISS_TP.S_TP.C_EL.212613", "异常的发送类型")
	}

	if model.SystemSmtpLogStatusPending != *log.Status {
		logger.SugarLogger(logger.WithRequestId(ctx), logger.WithStack()).Errorf("ISS_TP.S_TP.C_EL.252632 异常的日志状态，传入的日志状态为 %d", *log.Status)

		return errors.ErrorServerInternalError("ISS_TP.S_TP.C_EL.272638", "非待发送状态无法取消")
	}

	log.Status = pointer.New(model.SystemSmtpLogStatusCanceled)

	if err := cls.smtpLog.SaveSystemSmtpLog(ctx, log); nil != err {
		return err.Relation(errors.ErrorServerInternalError("ISS_TP.S_TP.C_EL.254450", "取消失败"))
	}

	return nil
}
